<template>
  <div class="register">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="register-box"
    >
      <h2 class="title">用户注册</h2>
      <el-form-item label="用户名" prop="phone">
        <el-input
          type="text"
          v-model="ruleForm.phone"
          autocomplete="off"
          placeholder="用户名"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input
          type="text"
          v-model="ruleForm.name"
          autocomplete="off"
          placeholder="姓名"
        ></el-input>
      </el-form-item>

      <el-form-item label="用户角色" prop="role">
        <el-select v-model="ruleForm.role" placeholder="请选择用户角色">
          <el-option
            v-for="item in rolelist"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item class="register-footer">
        <el-button type="primary" @click="submitForm()">注册</el-button>
        <el-button type="primary" @click="gologin()">已有账号，去登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get_register, get_role } from "../utlis/api";
export default {
  data() {
    //判断手机号
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户不能为空！"));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    //判断密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 6) {
        callback(new Error("请输入至少6位密码"));
      } else {
        callback();
      }
    };
    //确认密码
    var validateCheckPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一样"));
      } else {
        callback();
      }
    };
    //姓名
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入姓名"));
      } else if (/[\u4e00-\u9fa5]/.test(value) == false) {
        callback(new Error("请输入中文"));
      } else {
        callback();
      }
    };
    //角色
    var validateRole = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请选择一个角色"));
      } else {
        callback();
      }
    };
    return {
      rolelist: [], //角色列表
      ruleForm: {
        phone: "", //手机号
        pass: "", //密码
        checkPass: "", //确认密码
        name: "", //姓名
        role: "", //用户角色
      },
      rules: {
        //validator:验证器，    trigger：触发器
        phone: [{ validator: validatePhone, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validateCheckPass, trigger: "blur" }],
        name: [{ validator: validateName, trigger: "blur" }],
        role: [{ validator: validateRole, trigger: "blur" }],
      },
    };
  },
  mounted() {
    get_role().then((res) => {
      this.rolelist = res.data.list;
    });
  },
  methods: {
    gologin() {
      this.$router.push("/login");
    },
    submitForm() {
      //获取Form组件的实例, 调用实例的方法( validate内部会依次调用每一个 表单项的验证器 实现表单验证 )
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //发起登录请求
          var { checkPass, ...params } = this.ruleForm;

          get_register(params).then((res) => {
            console.log(res.data);
            if (res.data.code == 200) {
              this.$message({ message: res.data.msg, type: "success" });
            } else {
              this.$message({ message: res.data.msg, type: "error" });
            }
          });
        } else {
          console.log("注册失败");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.register {
  height: 100%;
  background: url(../assets/考勤管理-登录页-背景.png) no-repeat;
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .title {
    text-align: center;
    margin-bottom: 20px;
  }
  .register-box {
    background-color: #fff;
    border-radius: 10px;
    padding: 20px;
    min-width: 400px;
    .register-footer {
      display: flex;
      justify-content: center;
    }
  }
}
</style>